package cn.regexp.business.web.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.regexp.common.base.page.TablePageInfo;
import cn.regexp.common.web.AjaxResult;
import cn.regexp.domain.ShopGoodsBook;
import cn.regexp.query.GoodsBookQuery;
import cn.regexp.service.IGoodsBookService;
import cn.regexp.service.IWindowService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;

/**
 * 轮播图控制器
 */
@Slf4j
@Controller
@RequestMapping("business/goodsBook")
public class GoodsBookController {
    //模板前缀
    private static final String prefix = "business/goodsBook/";
    
    @Autowired
    private IGoodsBookService goodsBookService;
    @Autowired
    private IWindowService windowService;

    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("business:goodsBook:view")
    @RequestMapping("/listPage")
    public String listPage(Model model) {
        model.addAttribute("windows", windowService.list());
        return prefix + "list";
    }
    

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("business:goodsBook:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<ShopGoodsBook> query(GoodsBookQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        QueryWrapper<ShopGoodsBook> wrapper = new QueryWrapper<>();
        wrapper.like(ObjectUtil.isNotEmpty(qo.getKeyword()), "goods_name", qo.getKeyword())
                .eq(ObjectUtil.isNotEmpty(qo.getBookPoint()), "book_time", qo.getBookPoint())
                .eq(ObjectUtil.isNotEmpty(qo.getWindowId()), "window_id", qo.getWindowId())
                .apply(ObjectUtil.isNotEmpty(qo.getBookTime()),"date_format (book_time,'%Y-%m-%d') = date_format('" + DateUtil.date(qo.getBookTime()) + "','%Y-%m-%d')");
        return new TablePageInfo<>(goodsBookService.list(wrapper), goodsBookService.count(wrapper));
    }

    //删除
    @RequiresPermissions("business:goodsBook:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        goodsBookService.removeByIds(Collections.singletonList(ids));
        return AjaxResult.success();
    }
}
